package com.jmx.demo;

import com.jmx.demo2.First;
import com.jmx.demo2.Second;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;

public class HelloAgent implements NotificationListener {
    private MBeanServer mbs;
    //
    // public HelloAgent() {
    //     this.mbs = MBeanServerFactory.createMBeanServer("HelloAgent");
    //     HelloWorld hw = new HelloWorld();
    //     ObjectName helloWorldName = null;
    //     try {
    //         helloWorldName = new ObjectName("HelloAgent:name=helloWorld");
    //         // 此处将HelloWorld 绑定
    //         mbs.registerMBean(hw, helloWorldName);
    //     register();
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //     }
    //     startHtmlAdaptorServer();
    // }
    //
    // private void register() throws Exception {
    //     mbs.registerMBean(new First(), new ObjectName("HelloAgent:name=First"));
    //     mbs.registerMBean(new Second(), new ObjectName("HelloAgent:name=Second"));
    // }
    //
    // public void startHtmlAdaptorServer() {
    //     HtmlAdaptorServer htmlAdaptorServer = new HtmlAdaptorServer();
    //     ObjectName adapterName = null;
    //     try { // 多个属性使用,分隔
    //         adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092");
    //         htmlAdaptorServer.setPort(9092);
    //         // 将 HtmlAdaptorServer绑定
    //         mbs.registerMBean(htmlAdaptorServer, adapterName);
    //         htmlAdaptorServer.start();
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //     }
    // }
    //
    // public static void main(String args[]) {
    //     System.out.println(" hello agent is running");
    //     HelloAgent agent = new HelloAgent();
    // }

    @Override
    public void handleNotification(Notification notification, Object handback) {
        String message = notification.getMessage();
        System.out.println(message);
        System.out.println(handback);
    }
}